#include "variable.h"
#include "symbolTable.h"
#include "script.h"
#include "context.h"

using namespace wolv::condition;


Variable::Variable()
:loaded_(true)
{

}

Variable::Variable( const std::string& name )
:name_(name), loaded_(false), pContext_(0)
{

}

Variable::Variable( const VariableValue& value )
:value_(value), loaded_(true), pContext_(0)
{

}

bool Variable::operator==( const Variable& value )
{
	_Load();
	value._Load();

	return value_ == value.value_;
}

bool Variable::operator!=( const Variable& value )
{
	_Load();
	value._Load();

	return value_ != value.value_;
}

bool Variable::operator<( const Variable& value )
{
	_Load();
	value._Load();

	return value_ < value.value_;
}

bool Variable::operator<=( const Variable& value )
{
	_Load();
	value._Load();

	return value_ <= value.value_;
}

bool Variable::operator>( const Variable& value )
{
	_Load();
	value._Load();

	return value_ > value.value_;
}

bool Variable::operator>=( const Variable& value )
{
	_Load();
	value._Load();

	return value_ >= value.value_;
}

bool wolv::condition::Variable::in( const Variable& value )
{
	_Load();
	value._Load();

	return value_.in(value.value_);
}


int Variable::_Load() const
{
	if (loaded_)
	{
		return 0;
	}

	if (!pContext_)
	{
		return 0;
	}

	// try the best
	// if we could not find it, ignore it
	value_ = pContext_->GetSymbolTable()->GetSymbol(name_);
	if (value_.GetType() == wolv::weakType::nullType)
	{
		if (pContext_->GetScript())
		{
			pContext_->GetScript()->setError(SCRIPT_VARIABLE_NOT_FOUND, "SCRIPT_VARIABLE_NOT_FOUND", name_);
		}
	}

	return 0;
}

void wolv::condition::Variable::SetConext( LocalContext* pContext )
{
	pContext_ = pContext;
}


